<?
/**
 * This file is part of XNAT light.
 *
 * XNAT light is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * XNAT light is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with XNAT light.  If not, see <http://www.gnu.org/licenses/>.
 */
namespace NRG\Framework\Controllers;

use NRG\Framework\Exceptions\RouterException as RouterException;
use NRG\Framework\Exceptions\AuthException as AuthException;
use NRG\Framework\Common\SessionFactory as SessionFactory;
use NRG\Framework\Common\Registry as Registry;
use NRG\Framework\Views\View as View;

/**
 * Base controller
 *
 * @package NRG\Framework\Controllers
 */
class BaseController
{
    /**
     * View object
     *
     * @var View
     */
    protected $_view;

    /**
     * Controller name
     * 
     * @var string 
     */
    protected $_controller;
    
    /**
     * Action
     *
     * @var string
     */
    protected $_action;

    /**
     * Session object
     *
     * @var Session
     */
    protected $_session;
    
    /**
     * Base URL
     *
     * @var string
     */
    protected $_baseurl;

    /**
     * Constructor
     *
     * @param array $request
     * @throws RouterException
     */
    public function __construct(array $request)
    {
	if(!isset($request[0]))
	    $this->_action = "index";
	else
	    $this->_action = $request[0];

	$action = $this->_action . "Action";

	if(!method_exists($this, $action))
	{
	    $e = new RouterException(RouterException::NO_ACTION, $action);

	    $controller = new Error($e);
	}

	$this->_controller = $this->getName();

	$this->constructView();

	$this->_session = SessionFactory::getSession();

	$this->_baseurl = Registry::get("baseurl");
    }

    /**
     * Default index action
     */
    public function indexAction()
    {
	return;
    }

    /**
     * Overridable controller pre-dispatch method
     */
    public function preDispatch()
    {
	return;
    }

    /**
     * Overridable controller post-dispatch method
     */
    public function postDispatch()
    {
	return;
    }

    /**
     * Dispatch action
     */
    public function dispatch()
    {
	$action = $this->_action . "Action";

	// controller dispatch pipeline
	$this->preDispatch();
	$this->$action();
	$this->postDispatch();

	// render view
	$this->_view->render();
    }

    /**
     * Create view using controller and action name
     */
    protected function constructView()
    {
	// construct the view
	$this->_view = new View($this->_controller, $this->_action);
    }

    /**
     * Get the controller name without the namespace
     *
     * @return string
     */
    protected function getName()
    {
	$classname = get_class($this);

	$array = explode("\\", $classname);

	return $array[count($array) - 1];
    }

    /**
     * Call this at the beginning of any action to provide admin-only restriction
     */
    protected function adminOnly()
    {
	$user = $this->_xnat->getUser();

	if(trim(strtolower($user->getUsername())) != "admin")
	    throw new AuthException(AuthException::ADMIN_ONLY);
    }
}
